package startegy;

// 抽象策略接口：定义支付方式
interface PaymentStrategy {
    void pay(double amount);
}

// 具体策略类：支付宝支付
class AlipayStrategy implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("使用支付宝支付 " + amount + " 元");
    }
}

// 具体策略类：微信支付
class WeChatPayStrategy implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("使用微信支付 " + amount + " 元");
    }
}

// 上下文类：持有支付策略
class PaymentContext {
    private PaymentStrategy strategy;

    // 设置支付策略
    public void setPaymentStrategy(PaymentStrategy strategy) {
        this.strategy = strategy;
    }

    // 执行支付
    public void executePayment(double amount) {
        if (strategy == null) {
            System.out.println("请先选择支付方式！");
            return;
        }
        strategy.pay(amount);
    }
}

public class StrategyTest {
    public static void main(String[] args) {
        PaymentContext paymentContext = new PaymentContext();

        // 选择支付宝支付
        paymentContext.setPaymentStrategy(new AlipayStrategy());
        paymentContext.executePayment(100.0);  //使用支付宝支付 100.0 元

        // 选择微信支付
        paymentContext.setPaymentStrategy(new WeChatPayStrategy());
        paymentContext.executePayment(200.0);  //使用微信支付 200.0 元
    }
}